﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers {
    [RouteArea("admin")]
    [RoutePrefix("tokenUser")]
    public class TokenUserController : AdminController {
        private ITokenUserService _iTokenUserService;
        private IMerchantService _iMerchantService;
        private readonly IUserRedTransferService _iUserRedTransferService;
        public TokenUserController(ITokenUserService iTokenUserService, IMerchantService iMerchantService, IUserRedTransferService iUserRedTransferService) {
            _iTokenUserService = iTokenUserService;
            _iMerchantService = iMerchantService;
            _iUserRedTransferService = iUserRedTransferService;
        }

        public ActionResult Index() {
            return View();
        }

        public JsonResult Gets(TokenUserQuery query) {
            var model = _iTokenUserService.GetTokenUsers(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Get(long id, TokenUserPageQueryEx queryPageEx) {
            var model = _iTokenUserService.GetTokenUser(id, queryPageEx);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdateDisabled(long merchant_id, int isDisabled) {
            var result = _iTokenUserService.UpdateDisabled(merchant_id, isDisabled);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }

        public JsonResult GetTokenUserInvitationRanking(TokenUserInvitationQuery query) {
            if (string.IsNullOrEmpty(query.begin_time))
                query.begin_time = DateTime.Now.ToString("yyyy-MM-dd");
            if (string.IsNullOrEmpty(query.end_time))
                query.end_time = DateTime.Now.ToString("yyyy-MM-dd");
            var model = _iTokenUserService.GetTokenUserInvitationRanking(query);
            return Json(ResultToJson.Json(new { data = model, begin_time = query.begin_time, end_time = query.end_time }), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetTokenUserInvitationHis(long userId, string begin_time, string end_time) {
            if (string.IsNullOrEmpty(begin_time))
                begin_time = DateTime.Now.ToString("yyyy-MM-dd");
            if (string.IsNullOrEmpty(end_time))
                end_time = DateTime.Now.ToString("yyyy-MM-dd");
            var model = _iTokenUserService.GetTokenUserInvitationHis(userId, begin_time, end_time);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 增加用户马力
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="horsePower"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult IncreaseUserHorsePower(long userId, int horsePower, int day) {
            var result = _iTokenUserService.IncreaseUserHorsePower(userId, horsePower , day, currentUser.user_id);
            return Json(ResultToJson.Json(result ? " 增加成功" : "增加失败", result));
        }


        [HttpPost]
        public JsonResult UpdateUserType(long userId, int userType) {
            var result = _iTokenUserService.UpdateUserType(userId, userType);
            return Json(ResultToJson.Json(result ? " 修改成功" : "未做任何修改", result));
        }
        /// <summary>
        /// 修改推荐人
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="parentMobile"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateUserParent(long userId, string parentMobile) {
            var result = _iMerchantService.UpdateParent(userId, parentMobile, currentUser.user_id, t_RequestMethod.merchant_no);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "未做任何修改", result > 0));
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Save(token_web_register_model model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            var result = _iMerchantService.TokenWebRegister(model);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result));

        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdatePwd(token_modify_pwd_model model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            var result = _iMerchantService.TokenModifyPwd(model);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result));

        }
        /// <summary>
        /// 转红钻
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userType"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UserRedTransfer(user_red_transfer_model_ex model) {
            bool result = _iUserRedTransferService.UserRedTransfer(new user_red_transfer_model { receiver_mobile = model.receiver_mobile, amount = model.amount }, model.user_id, true, model.is_show_transfer_out);
            return Json(ResultToJson.Json(result ? "转赠成功" : "转赠失败", result));
        }


        #region 实名审核
        public JsonResult GetAuthInfos(MemberQuery query) {
            var model = _iMerchantService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetAuthInfo(long id) {
            var model = _iMerchantService.GetAuthById(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult AuthInfoAudit(authinfo_dto dto) {
            var result = _iMerchantService.AuthInfoAudit(dto, currentUser.user_id);
            return Json(ResultToJson.Json(result ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 重置实名认证次数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ResetAuthNum(long id) {
            var result = _iMerchantService.ResetAuthNum(id);
            return Json(ResultToJson.Json(result ? " 重置成功" : "重置失败", result));
        }
        #endregion
    }
}